const express = require('express');//引入express
const logger = require("morgan");
const favicon = require("serve-favicon");
const path = require('path');
//post请求模块
const bodyParser = require("body-parser");
//引入自己写的路由模块
const route = require(path.join(__dirname,"./routes/Router"));
const app = express();
const cors = require('cors');
app.use(favicon(path.join(__dirname, './public/images', 'favicon.ico')))//小图标
app.use(cors());//跨域请求
app.use(logger("dev"))//调用日志，配置为dev（开发）模式
//配置post请求必须写在路由上方
app.use(bodyParser.urlencoded({limit:'5mb', extended: true }));
app.use(bodyParser.json({limit:'5mb'}));

app.use(route)
app.use('/test',(req, res)=>{
    res.send({id:1,name:"zs"})
})


//地址没有时的use
app.use((req, res,next) => {
    res.status(404);
    res.send('<h1>404 not found</h1>')
    // resp.sendFile(__dirname + "/public/html/404.html");

    // resp.sendFile(path.
    next();
})
//错误级别的中间件 放在所有路由之后
app.use((err, req, res, next) => {
    console.log('发生错误' + err.message);
    res.send('error' + err.message)
    next()
});
app.listen(80, () => {
    console.log('服务器启动在 http://127.0.0.1');
})